package com.ihcy.base.utils;

import java.lang.reflect.Field;
import java.util.Objects;

public class EnumUtils {

    /**
     * 通用枚举类，根据 code 获取 对应的枚举
     */
    public static <E> E getByCode(Class<E> clz, String code) {
        try {
            for (E e : clz.getEnumConstants()) {
                Field field = e.getClass().getDeclaredField("code");
                field.setAccessible(true);
                if (Objects.equals(field.get(e), code)) {
                    return e;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
